package com.nnnu.wsnackshop.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.BannerDTO;
import com.nnnu.wsnackshop.pojo.vo.BannerVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IBannersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图管理Controller（管理端）
 */
@RestController("adminBannersController")
@RequestMapping("/api/admin/banners")
@Tag(name = "轮播图管理接口")
@RequiredArgsConstructor
public class BannersController {
    private static final Logger log = LoggerFactory.getLogger(BannersController.class);
    private final IBannersService bannersService;

    /**
     * 分页查询轮播图
     */
    @RequiresPermissions("banner:list")
    @GetMapping("/page")
    @Operation(description = "分页查询轮播图")
    public Result<PageResult<BannerVO>> getBannerPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String linkType) {

        Page<com.nnnu.wsnackshop.pojo.entity.Banners> page = new Page<>(current, size);
        var result = bannersService.getBannerPage(page, title, status, linkType);

        PageResult<BannerVO> pageResult = new PageResult<>();
        pageResult.setItems(result.getRecords());
        pageResult.setTotal(result.getTotal());

        return Result.success(pageResult);
    }

    /**
     * 获取所有轮播图列表
     */
    @RequiresPermissions("banner:list")
    @GetMapping("/list")
    @Operation(description = "获取所有轮播图列表")
    public Result<List<BannerVO>> getAllBanners() {
        List<com.nnnu.wsnackshop.pojo.entity.Banners> banners = bannersService.list();
        List<BannerVO> bannerVOs = banners.stream().map(banner -> {
            BannerVO vo = new BannerVO();
            org.springframework.beans.BeanUtils.copyProperties(banner, vo);
            return vo;
        }).collect(java.util.stream.Collectors.toList());

        return Result.success(bannerVOs);
    }

    /**
     * 根据ID获取轮播图详情
     */
    @RequiresPermissions("banner:read")
    @GetMapping("/{id}")
    @Operation(description = "根据ID获取轮播图详情")
    public Result<BannerVO> getBannerById(@PathVariable Integer id) {
        BannerVO banner = bannersService.getBannerById(id);
        if (banner == null) {
            return Result.error(404, "轮播图不存在");
        }
        return Result.success(banner);
    }

    /**
     * 添加轮播图
     */
    @RequiresPermissions("banner:add")
    @PostMapping
    @Operation(description = "添加轮播图")
    public Result<?> addBanner(@RequestBody BannerDTO bannerDTO) {
        log.info("添加轮播图: {}", bannerDTO);
        bannersService.addBanner(bannerDTO);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 更新轮播图
     */
    @RequiresPermissions("banner:update")
    @PutMapping
    @Operation(description = "更新轮播图")
    public Result<?> updateBanner(@RequestBody BannerDTO bannerDTO) {
        log.info("更新轮播图：{}", bannerDTO);
        bannersService.updateBanner(bannerDTO);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 删除轮播图
     */
    @RequiresPermissions("banner:delete")
    @DeleteMapping("/{id}")
    @Operation(description = "删除轮播图")
    public Result<String> deleteBanner(@PathVariable Integer id) {
        bannersService.deleteBanner(id);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除轮播图
     */
    @RequiresPermissions("banner:delete")
    @DeleteMapping("/batch")
    @Operation(description = "批量删除轮播图")
    public Result<String> deleteBanners(@RequestBody List<Integer> ids) {
        bannersService.deleteBanners(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 更新轮播图状态
     */
    @RequiresPermissions("banner:update")
    @PutMapping("/{id}/status")
    @Operation(description = "更新轮播图状态")
    public Result<String> updateBannerStatus(@PathVariable Integer id, @RequestParam Integer status) {
        bannersService.updateBannerStatus(id, status);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 更新轮播图排序
     */
    @RequiresPermissions("banner:update")
    @PutMapping("/{id}/sort")
    @Operation(description = "更新轮播图排序")
    public Result<String> updateBannerSort(@PathVariable Integer id, @RequestParam Integer sortOrder) {
        bannersService.updateBannerSort(id, sortOrder);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}